<#assign pageName="新增请假申请">
<@override name="head-styles">
</@override>
<@override name="page-content">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>${pageName}</legend>
    </fieldset>
    <form class="layui-form padding-10" lay-filter="formOvertimeAdd">
        <@ctrl.Textbox path="bsLeaveInfo.depName" labelText="所在部门" required="required" attributes="readonly"/>
        <@ctrl.Textbox path="bsLeaveInfo.empName" labelText="员工名称" required="required" attributes="readonly"/>
        <@ctrl.SingleSelect path="bsLeaveInfo.leaveType" labelText="请假类型" 
            options=leaveTypeList text="name" value="id" required="required">
            <option value="">—请选择—</option>
        </@ctrl.SingleSelect>
         <@ctrl.SingleSelect path="bsLeaveInfo.leaveCategory" labelText="请假类别" 
            options={"0":"按天请","1":"按小时请"} text="1" value="0" required="required">
            <option value="">—请选择—</option>
        </@ctrl.SingleSelect>
        <div id="forDate" style="display:none;">
            <@ctrl.Date path="bsLeaveInfo.startDate" labelText="开始时间"/>
            <@ctrl.Date path="bsLeaveInfo.endDate" labelText="结束时间" />
        </div>
        <div id="forDateTime">
            <@ctrl.Date path="bsLeaveInfo.startDateT" labelText="开始时间" />
            <@ctrl.Date path="bsLeaveInfo.endDateT" labelText="结束时间" />
        </div>
        <div id="forDay">
            <@ctrl.Textbox path="bsLeaveInfo.leaveDays" labelText="请假天数" unit="天"/>
        </div>
        <div id="forHour" style="display:none;">
            <@ctrl.Textbox path="bsLeaveInfo.leaveHour" labelText="请假小时数" unit="小时"/>
        </div>             
        <@ctrl.Textbox path="bsLeaveInfo.sendUname" labelText="抄送人" required="required"/>  
        <@ctrl.Hidden path="bsLeaveInfo.sendUid"/> 
        <@ctrl.Textarea path="bsLeaveInfo.reason" labelText="请假事由" required="required"/>        
        <@ctrl.Textarea path="bsLeaveInfo.remark" labelText="备注"/>
        <@ctrl.Hidden path="bsLeaveInfo.depId"/>
        <@ctrl.Hidden path="bsLeaveInfo.empId"/>
        <@ctrl.FormAction backURL="manage"/>
   </form>
</@override>
<@override name="page-scripts">
    <script>
        $(function(){
            layui.use(['form','layer','laydate','jquery'], function(){
                var form = layui.form;
                var layer=layui.layer;
                var laydate = layui.laydate;
                $=layui.jquery;
                laydate.render({
                    elem: '#startDate'
                    ,type: 'date'
                    ,format: 'yyyy-MM-dd'
                    ,trigger: 'click'
                       ,done: (value) => {                      
                           type = $("#leaveCategory").val();
                           if(type != ""){
                               endDate = $("#endDate").val();
                               startDate = value;
                               depId = $("#depId").val();
                               if(endDate != ""){
                                   $.ajax({
                                       type:'post',
                                       url : '${rc.getContextPath()}/attendance/leave/apply/calculationDay',
                                       data:{startDate:startDate,endDate:endDate,depId:depId},
                                       contentType: "application/x-www-form-urlencoded; charset=utf-8",
                                       success: function(time){
                                           if(time != ""){
                                               $("#leaveDays").val(time);
                                           }else{
                                               alert("开始时间和结束时间填写有误！！");                        
                                           }                                       
                                       }
                                   })                                      
                               }                              
                           }else{
                               alert("你还未选择请假类别！！");
                           }                           
                    } 
                });
                laydate.render({
                    elem: '#endDate'
                    ,type: 'date'
                    ,format: 'yyyy-MM-dd'
                    ,trigger: 'click'
                       ,done: (value) => {
                           type = $("#leaveCategory").val();                          
                           if(type != ""){
                           startDate = $("#startDate").val();
                        endDate = value;
                        depId = $("#depId").val();
                               if(startDate == "" || endDate == ""){
                                   alert("开始时间和结束时间不能为空！");
                               }else{
                                   $.ajax({
                                       type:'post',
                                       url : '${rc.getContextPath()}/attendance/leave/apply/calculationDay',
                                       data:{startDate:startDate,endDate:endDate,depId:depId},
                                       contentType: "application/x-www-form-urlencoded; charset=utf-8",
                                       success: function(time){
                                           if(time != ""){
                                               $("#leaveDays").val(time);
                                           }else{
                                               alert("开始时间和结束时间填写有误！！");                        
                                           }                                               
                                       }
                                   })                                
                               }
                           }else{
                               alert("你还未选择请假类别！！");
                           }
                    }
                });
                laydate.render({
                    elem: '#startDateT'
                    ,type: 'datetime'
                    ,format: 'yyyy-MM-dd HH:mm'
                    ,trigger: 'click'
                       ,done: function(value, date){ //监听日期被切换
                           type = $("#leaveCategory").val();
                           console.log("type"+type);
                           if(type != ""){
                               startDate = value;
                               console.log("startDate"+startDate);
                               endDate = $("#endDateT").val();
                               console.log("endDate"+endDate);
                               depId = $("#depId").val();
                               if(endDate != ""){
                                   $.ajax({
                                       type:'post',
                                       url : '${rc.getContextPath()}/attendance/leave/apply/calculationHour',
                                       data:{startDate:startDate,endDate:endDate,depId:depId,},
                                       contentType: "application/x-www-form-urlencoded; charset=utf-8",
                                       success: function(time){
                                           if(time != ""){
                                               $("#leaveHour").val(time);
                                           }else{
                                               $("#leaveHour").val("");
                                               alert("开始时间或结束时间填写有误！！（开始时间和结束时间必须为上班时间）");                                               
                                           }                                                   
                                       }
                                   })                        
                               }                                       
                           }else{
                               alert("你还未选择请假类别！！");
                           }                       
                       } 
                });
                laydate.render({
                    elem: '#endDateT'
                    ,type: 'datetime'
                    ,format: 'yyyy-MM-dd HH:mm'
                    ,trigger: 'click'
                       ,done: function(value, date){ //监听日期被切换
                           startDate = $("#startDateT").val();
                           endDate = value;
                           depId = $("#depId").val();
                           if(startDate == "" || endDate == ""){
                               alert("开始时间和结束时间不能为空！");
                           }else{
                               $.ajax({
                                   type:'post',
                                   url : '${rc.getContextPath()}/attendance/leave/apply/calculationHour',
                                   data:{startDate:startDate,endDate:endDate,depId:depId,},
                                   contentType: "application/x-www-form-urlencoded; charset=utf-8",
                                   success: function(time){
                                       console.log(time);
                                       if(time != ""){
                                           $("#leaveHour").val(time);
                                       }else{
                                           $("#leaveHour").val("");
                                           alert("开始时间和结束时间填写有误请检查！（开始时间和结束时间必须为上班时间）");
                                           
                                       }                                               
                                   }
                               })                                
                           }
                       } 
                });
                //监听提交
                form.on('submit(submit)', function(data){
                    submitData(data.field, '${contextPath}/workflow/leave/apply/add', function(){location.href="${contextPath}/workflow/leave/apply/manage";});
                    return false;
                });
                form.on('select(sendDep)',function(data){
                    var cityValue =  data.value;
                    var $region = "<option value=''>请选择抄送人</option>";
                    if(cityValue!=""){
                        $.post('${contextPath}/meeting/roomapply/apply/type/'+cityValue+'/query', {}, 
                        function(data){
                            $.each(data, function(index, item){
                                $region += "<option value='" + item.userId + "'>" + item.uname + "</option>";
                            })
                            $("#sendUid").html($region);
                            form.render("select");
                        })
                    }else{
                        $("#sendUid").html($region);
                        form.render("select");
                    }
                    $("#mendDep").val(cityValue);
                    $("#sendUid").trigger("change");
                });
                form.on('select(leaveCategory)',function(data){
                    var CValue =  data.value;
                    if(CValue == "0"){//按天请
                        $("#forDateTime").attr("style","display:none;");//隐藏div
                        $("#forHour").attr("style","display:none;");//隐藏div
                        $("#forDate").attr("style","display:block;");//显示
                        $("#forDay").attr("style","display:block;");//显示
                        $("#startDateT").val();
                        $("#endDateT").val();
                    }
                    if(CValue == "1"){//按天请
                        $("#forDate").attr("style","display:none;");//隐藏div
                        $("#forDateTime").attr("style","display:block;");//显示
                        $("#forDay").attr("style","display:none;");//隐藏div
                        $("#forHour").attr("style","display:block;");//显示
                        $("#startDate").val();
                        $("#endDate").val();
                    }
                });
            });
        })
        function calculationD() {
            startDate = $("#startDate").val();
            endDate = $("#endDate").val();
            depId = $("#depId").val();
            if(startDate == "" || endDate == ""){
                alert("开始时间和结束时间不能为空！");
            }else{
                $.ajax({
                    type:'post',
                    url : '${rc.getContextPath()}/attendance/leave/apply/calculationDay',
                    data:{startDate:startDate,endDate:endDate,depId:depId},
                    contentType: "application/x-www-form-urlencoded; charset=utf-8",
                    success: function(time){
                        if(time != ""){
                            $("#leaveDays").val(time);
                        }else{
                            alert("开始时间和结束时间填写有误！！");                        
                        }                                               
                    }
                })                                
            }             
        }; 
         function calculationH() {
            startDate = $("#startDateT").val();
            endDate = $("#endDateT").val();
            depId = $("#depId").val();
            if(startDate == "" || endDate == ""){
                alert("开始时间和结束时间不能为空！");
            }else{
                $.ajax({
                    type:'post',
                    url : '${rc.getContextPath()}/attendance/leave/apply/calculationHour',
                    data:{startDate:startDate,endDate:endDate,depId:depId,},
                    contentType: "application/x-www-form-urlencoded; charset=utf-8",
                    success: function(time){
                        if(time != ""){
                            $("#leaveHour").val(time);
                        }else{
                            alert("开始时间和结束时间填写有误！！");                        
                        }                                               
                    }
                })                                
            }             
        };
         $("#sendUname").mousedown(function() {
            var ids = $('#sendUid').val();
            layui.use('layer', function() {
                layer.open({
                    type : 2,
                    title : false,
                    area : [ '60%', '70%' ],
                    content : '${contextPath}/common/showusers?ids='+ids    
                });
            });
        })
        function shareU(id,name){
             $('#sendUid').val(id);
             $('#sendUname').val(name);
        };
    </script>
</@override>
<@extends name="/layout/basepage.html"/> 
